package cn.com.api.service.impl;

import cn.com.api.entity.U_label;
import cn.com.api.entity.Ylj_All_Label_Obj;
import cn.com.api.mapper.U_labelMapper;
import cn.com.api.service.U_labelService;
import cn.com.api.service.impl.base.U_labelServiceImplBase;
import cn.com.util.DateUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class U_labelServiceImpl extends U_labelServiceImplBase implements U_labelService {
	@Autowired
	private U_labelMapper U_labelMapper;

	public List<Ylj_All_Label_Obj> findAllU_labels() {
		List<Ylj_All_Label_Obj> result = new ArrayList<Ylj_All_Label_Obj>();

		List<U_label> list = this.U_labelMapper.findByParentId(-1);
		for (int i = 0; i < list.size(); i++) {
			Ylj_All_Label_Obj obj = new Ylj_All_Label_Obj();

			String label_id = ((U_label) list.get(i)).getId().toString();
			obj.setLabel_id(Integer.valueOf(label_id));
			obj.setLabel_name(((U_label) list.get(i)).getLabel_name());

			List<U_label> list2 = this.U_labelMapper.findByParentId(Integer.valueOf(label_id));
			if (list.size() == 0) {
				obj.setChild(new ArrayList<Ylj_All_Label_Obj>());
				result.add(obj);
			} else {
				List<Ylj_All_Label_Obj> childs = new ArrayList<Ylj_All_Label_Obj>();
				for (int j = 0; j < list2.size(); j++) {
					Ylj_All_Label_Obj obj2 = new Ylj_All_Label_Obj();

					// String label_id2 = ((U_label) list2.get(j)).getId();
					obj2.setLabel_id(Integer.valueOf(label_id));
					obj2.setLabel_name(((U_label) list2.get(j)).getLabel_name());
					obj2.setChild(new ArrayList<Ylj_All_Label_Obj>());
					childs.add(obj2);
				}
				obj.setChild(childs);
				result.add(obj);
			}
		}
		return result;
	}

	public List<Ylj_All_Label_Obj> findLikeU_labels(String keyword) {
		List<Ylj_All_Label_Obj> result = new ArrayList<Ylj_All_Label_Obj>();

		List<U_label> list = this.U_labelMapper.findByParentId(-1);
		for (int i = 0; i < list.size(); i++) {
			Ylj_All_Label_Obj obj = new Ylj_All_Label_Obj();

			String label_id = ((U_label) list.get(i)).getId().toString();
			obj.setLabel_id(Integer.valueOf(label_id));
			String str_label_name = ((U_label) list.get(i)).getLabel_name();
			obj.setLabel_name(((U_label) list.get(i)).getLabel_name());

			if (keyword.contains(str_label_name)) {
				List<U_label> list2 = this.U_labelMapper.findByParentId(Integer.valueOf(label_id));
				if (list.size() == 0) {
					obj.setChild(new ArrayList<Ylj_All_Label_Obj>());
					result.add(obj);
				} else {
					List<Ylj_All_Label_Obj> childs = new ArrayList<Ylj_All_Label_Obj>();
					for (int j = 0; j < list2.size(); j++) {
						Ylj_All_Label_Obj obj2 = new Ylj_All_Label_Obj();

						String label_id2 = ((U_label) list2.get(j)).getId().toString();
						obj2.setLabel_id(Integer.valueOf(label_id));
						obj2.setLabel_name(((U_label) list2.get(j)).getLabel_name());
						obj2.setChild(new ArrayList<Ylj_All_Label_Obj>());
						childs.add(obj2);
					}
					obj.setChild(childs);
					result.add(obj);
				}
			}
		}
		return result;
	}

	public List<U_label> findAllLabels(Map map) {
		return this.U_labelMapper.findAllLabels(map);
	}

	public List<U_label> getLabelsByMap(Map map) {
		return this.U_labelMapper.getLabelsByMap(map);
	}

	public List<U_label> getAlllabels(int type) {
		List<U_label> labList = new ArrayList<U_label>();
		try {
			List<U_label> parentList = this.U_labelMapper.findParentByType(type);
			for (U_label lab : parentList) {
				List<U_label> childList = this.U_labelMapper.findByParentId(lab.getId());
				lab.setChildLabel(childList);
				labList.add(lab);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return labList;
	}

	public List<U_label> getTypeParentLabels(String type) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("parent_id", -1);
		List<U_label> list = this.U_labelMapper.getLabelsByMap(map);
		return list;
	}

	public void addOrUpdatelabel(U_label u_label) {
		if (u_label.getId() != null && !"".equals(u_label.getId())) {// 修改
			U_label label = this.U_labelMapper.findU_labelById(u_label.getId());
			label.setLabel_name(u_label.getLabel_name());
			label.setParent_id(u_label.getParent_id());
			label.setType(u_label.getType());
			label.setSort(u_label.getSort());
			this.U_labelMapper.updateU_label(label);
		} else {
			u_label.setCreate_time(DateUtil.nowTimeToString());
			this.U_labelMapper.insertU_label(u_label);
		}
	}
}
